var utils = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    arr: [], //放置返回数据的数组  
    pageIndex: 1,   // 设置加载的第几次，默认是第一次  
    pageSize: 10,      //返回数据的个数  
    nodata: true,  //“没有数据”的变量，默认false，隐藏
    status:0,
    dominameImg: utils.dominameImg
  },
  onLoad:function(options){
    this.setData({
      status: options.type
    });
  },
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1,
      hasData: true
    });
    this.ajax();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    if (this.data.hasData) {
      this.ajax();
    }
  },
  ajax: function (e) {
    let that = this;
    //wx.showLoading();
    wx.request({
      url: utils.dominame + '/api/v1/myself/myOrderList',
      data: {
        token: app.globalData.token,
        order_status: that.data.status,
        pageSize: that.data.pageSize,
        pageIndex: that.data.pageIndex
      },
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var arrBox;
          if (that.data.pageIndex == 1) {
            arrBox = res.data.data.rows;
          } else {
            arrBox = that.data.arr.concat(res.data.data.rows)
          };
          if (that.data.pageSize * that.data.pageIndex < res.data.data.recordCount) {
            var num = that.data.pageIndex + 1;
            that.setData({
              arr: arrBox,
              hasData: true,
              pageIndex: num
            });
          } else {
            that.setData({
              arr: arrBox,
              hasData: false
            });
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      },
      fail: function (res) { },
      complete: function (res) {
       // wx.hideLoading();
      },
    })
  },
  makeSure:function(e){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否确认收货？',
      success:function(res){
        if(res.confirm){
          wx.request({
            url: utils.dominame + '/api/v1/order/confirmReceipt',
            data: {
              token: app.globalData.token,
              order_id:e.currentTarget.id,
              channel:2
            },
            header: { "Content-Type": "application/x-www-form-urlencoded" },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              console.log(res);
              if (res.data.code == 0) {
                that.setData({
                  pageIndex: 1,
                  hasData: true
                });
                that.ajax();
              }
            },
            fail: function (res) { },
            complete: function (res) {
              // wx.hideLoading();
            },
          })
        }
      }
    })
  },
  goPay:function(e){
    wx.navigateTo({
      url: '../../payChoose/payChoose?order_id=' + e.currentTarget.id + '&actual=' + e.currentTarget.dataset.actual,
    })
  },
  goDetail:function(e){
    wx.navigateTo({
      url: '../orderDetail/orderDetail?orderId='+e.target.id,
    })
  },
  tabBarTap:function(e){
    if (e.currentTarget.dataset.controlitemid!=this.data.status){
      this.setData({
        pageIndex: 1,
        hasData: true,
        arr: [],
        status: e.currentTarget.dataset.controlitemid
      });
      this.ajax();
    }
  },
  onShow: function () {
    this.ajax();
  },
})